package com.cgnpc.scp.achievement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.achievement.mybatis.entity.*;
import com.cgnpc.scp.achievement.mybatis.mapper.client.SubjecFileMapper;
import com.cgnpc.scp.achievement.mybatis.mapper.client.SubjecUserMapper;
import com.cgnpc.scp.achievement.mybatis.mapper.client.SubjectMapper;
import com.cgnpc.scp.achievement.pojo.SaveSubjectReq;
import com.cgnpc.scp.achievement.pojo.SubjectDetail;
import com.cgnpc.scp.achievement.service.SubjectService;
import com.cgnpc.scp.common.bean.CreateFileInfo;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.service.AppConstantService;
import com.cgnpc.scp.common.service.ExcelCustomTemplateService;
import com.cgnpc.scp.common.utils.*;
import com.cgnpc.scp.manager.mybatis.entity.DataDict;
import com.cgnpc.scp.manager.service.IDataDictService;
import com.cgnpc.scp.purchase.mybatis.entity.PurchaseProject;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
public class SubjectServiceImpl implements SubjectService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SubjectServiceImpl.class);

    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private IDataDictService dataDictService;

    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private SubjecUserMapper subjecUserMapper;

    @Autowired
    private SubjecFileMapper subjecFileMapper;

    @Autowired
    private AppConstantService appConstantService;

    @Autowired
    private SqlSession sqlSession;
    @Autowired
    private ExcelCustomTemplateService excelCustomTemplateService;

    @Override
    @Transactional
    public Subject saveSubject(SaveSubjectReq saveReq) {
        Subject re = null;

        try {
            Subject subject = saveReq.getSubject();
            boolean isNew = false;
            UserInfo userinfo = currentUser.getCurrentUserDetail();
            if (subject.getId() == null) {
                subject.setId(IDHelper.uuid());
                subject.setCreateUser(userinfo.getUserId());
                subject.setCreateUserName(userinfo.getUserName());

                subjectMapper.insert(subject);
                isNew = true;
            } else {
                subject.setLastUpdateTime(new Date());
                subject.setLastUpdateUser(userinfo.getUserId());
                subject.setLastUpdateUserName(userinfo.getUserName());

                subjectMapper.updateById(subject);
            }

            List<SubjecUser> subjecUsers = saveReq.getSubjecUsers();
            if (!isNew) {
                /*SubjecUserExample example = new SubjecUserExample();
                SubjecUserExample.Criteria criteria = example.createCriteria();
                criteria.andSubjectIdEqualTo(subject.getId());*/
                QueryWrapper<SubjecUser> queryWrapper = new QueryWrapper();
                queryWrapper.eq("subject_id", subject.getId());
                subjecUserMapper.delete(queryWrapper);
            }
            for (SubjecUser curData : subjecUsers) {
                curData.setId(IDHelper.uuid());
                curData.setSubjectId(subject.getId());
                subjecUserMapper.insert(curData);
            }

            List<SubjecFile> sSubjecFiles = saveReq.getSubjecFiles();
            if (!isNew) {
                /*SubjecFileExample example = new SubjecFileExample();
                SubjecFileExample.Criteria criteria = example.createCriteria();
                criteria.andSubjectIdEqualTo(subject.getId());*/
                QueryWrapper<SubjecFile> queryWrapper = new QueryWrapper();
                queryWrapper.eq("subject_id", subject.getId());
                subjecFileMapper.delete(queryWrapper);
            }
            for (SubjecFile curData : sSubjecFiles) {
                curData.setId(IDHelper.uuid());
                curData.setSubjectId(subject.getId());
                subjecFileMapper.insert(curData);
            }

            re = subject;
        } catch (Exception e) {
            LOGGER.error("SubjectServiceImpl save failed:", e);
        }

        return re;
    }

    @Override
    public MyPageResult<Subject> querySubjectPageList(Subject subject) {
        MyPageResult<Subject> re = null;

        int pageNum = subject.getPageNum();
        int pageSize = subject.getPageSize();
        Page<Subject> queryPage = new Page<>(pageNum, pageSize);

        //SubjectExample example = new SubjectExample();
        QueryWrapper<Subject> queryWrapper = new QueryWrapper();
        if (subject.getSubjectName() != null) {
            /*SubjectExample.Criteria criteria1 = example.createCriteria();
            criteria1.andSubjectCodeLike(SqlUtil.valueJoinLikeBoth(subject.getSubjectName()));
            SubjectExample.Criteria criteria2 = example.or();
            criteria2.andSubjectNameLike(SqlUtil.valueJoinLikeBoth(subject.getSubjectName()));*/
            queryWrapper.like("subject_code", subject.getSubjectCode());
            queryWrapper.like("subject_name", subject.getSubjectName());
        }
        if (subject.getYear() != null) {
            queryWrapper.like("year", subject.getYear());
        }
        if (subject.getLevel() != null) {
            queryWrapper.like("level", subject.getLevel());
        }
        //List<Subject> subjects = subjectMapper.selectByExample(example);
        IPage<Subject> subjectPageInfo = subjectMapper.selectPage(queryPage, queryWrapper);

        re = MyPageResultUtil.createMyPageResultPlus(subjectPageInfo);

        return re;
    }

    @Override
    public SubjectDetail querySubjectDetailById(String id) {
        SubjectDetail re = null;

        Subject subject = subjectMapper.selectById(id);
        if (subject != null) {
            re = new SubjectDetail();
            re.setSubject(subject);

            /*SubjecUserExample userExample = new SubjecUserExample();
            SubjecUserExample.Criteria userCriteria = userExample.createCriteria();
            userCriteria.andSubjectIdEqualTo(id);*/
            QueryWrapper<SubjecUser> queryWrapper = new QueryWrapper();
            queryWrapper.eq("subject_id", subject.getId());
            List<SubjecUser> subjecUsers = subjecUserMapper.selectList(queryWrapper);
            re.setSubjecUsers(subjecUsers);

            /*SubjecFileExample fileExample = new SubjecFileExample();
            SubjecFileExample.Criteria fileCriteria = fileExample.createCriteria();
            fileCriteria.andSubjectIdEqualTo(id);*/
            QueryWrapper<SubjecFile> queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("subject_id", subject.getId());
            List<SubjecFile> subjecFiles = subjecFileMapper.selectList(queryWrapper2);
            re.setSubjecFiles(subjecFiles);
        }

        return re;
    }

    @Override
    public boolean delByIds(List<String> ids) {
        boolean re = false;

        /*SubjectExample example = new SubjectExample();
        SubjectExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);*/
        subjectMapper.deleteBatchIds(ids);
        re = true;

        return re;
    }

    @Override
    public File genDownLoadFiles(String id) {
        File re = null;

        /*SubjecFileExample example = new SubjecFileExample();
        SubjecFileExample.Criteria criteria = example.createCriteria();
        criteria.andSubjectIdEqualTo(id);

        List<SubjecFile> subjecFiles = subjecFileMapper.selectByExample(example);*/
        QueryWrapper<SubjecFile> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("subject_id", id);
        List<SubjecFile> subjecFiles = subjecFileMapper.selectList(queryWrapper2);
        if (subjecFiles.size() > 0) {
            File temFile = null;
            List<File> temFiles = new ArrayList<>(subjecFiles.size());
            for (SubjecFile curData : subjecFiles) {
                temFile = FileUtil.createSafeFileToInsideLocalDir(appConstantService, curData.getFileName());
                boolean downResult = downLoadFromFileCenter(temFile, curData.getFilePath());
                if (downResult)
                    temFiles.add(temFile);
            }
            if (temFiles.size() > 0) {
                if (temFiles.size() == 1)
                    re = temFiles.get(0);
                else
                    re = genZipFile(temFiles);
            }
        }

        return re;
    }

    private boolean downLoadFromFileCenter(File temFile, String url) {
        boolean re = false;

        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            URL urls = new URL(url);
            URLConnection conn = urls.openConnection();
            inStream = conn.getInputStream();
            fs = new FileOutputStream(temFile);

            byte[] buffer = new byte[1024];
            int byteread = 0;
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            re = true;
        } catch (IOException e) {
            LOGGER.error("downLoadFromFileCenter failed:", e);
        } finally {
//            try {
//                if (inStream != null) inStream.close();
//                if (fs != null) fs.close();
//            } catch (IOException e) {
//                LOGGER.error("downLoadFromFileCenter close stream failed:", e);
//            }

            IOUtil.closeStream(inStream);
            IOUtil.closeStream(fs);
        }

        return re;
    }

    private File genZipFile(List<File> temFiles) {
        UserInfo userinfo = currentUser.getCurrentUserDetail();
        File re = FileUtil.createSafeFileToInsideLocalDir(appConstantService, userinfo.getUserName() + System.currentTimeMillis() + ".zip");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fileOutputStream = null;
        ZipOutputStream out = null;
        try {
            fileOutputStream = new FileOutputStream(re);
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            out = new ZipOutputStream(cos);

            for (File curData : temFiles) {
                fis = new FileInputStream(curData);
                bis = new BufferedInputStream(fis);
                ZipEntry entry = new ZipEntry(curData.getName());
                out.putNextEntry(entry);
                int count;
                byte data[] = new byte[1024];
                while ((count = bis.read(data)) != -1) {
                    out.write(data, 0, count);
                }
                bis.close();
                fis.close();
                FileUtil.delete(curData);
            }
        } catch (IOException e) {
            LOGGER.error("genZipFile failed:", e);
        } finally {
            try {
                if (out != null) out.close();
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                LOGGER.error("genZipFile close stream failed:", e);
            }
        }

        return re;
    }

    @Override
    public boolean isAllowDownload(String id) {
        boolean re = false;

        /*SubjecUserExample userExample = new SubjecUserExample();
        SubjecUserExample.Criteria userCriteria = userExample.createCriteria();
        userCriteria.andSubjectIdEqualTo(id);
        List<SubjecUser> subjecUsers = subjecUserMapper.selectByExample(userExample);*/
        QueryWrapper<SubjecUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("subject_id", id);
        List<SubjecUser> subjecUsers = subjecUserMapper.selectList(queryWrapper);
        if (subjecUsers.size() > 0) {
            UserInfo userinfo = currentUser.getCurrentUserDetail();
            for (SubjecUser curData : subjecUsers) {
                if (curData.getParticipantId().equals(userinfo.getUserId())) {
                    re = true;
                    break;
                }
            }
        } else
            re = true;

        return re;
    }
    @Override
    public <T extends Subject> CreateFileInfo exportExcel(T reqEntity) throws Exception{
        final String logInfo = "[exportExcel_UUID_" + UUID.randomUUID() + "]";
        CreateFileInfo createFileInfo = null;

        try {
            // 创建queryExample
            SubjectExample subjectExample = createQueryExample(reqEntity);
            // mappedStatementNamespace为mybatis生成的mapper.xml中的命名空间
            String mappedStatementNamespace = "com.cgnpc.scp.achievement.mybatis.mapper.client.SubjectMapper";
            // mappedStatementMethod为mybatis生成的mapper.xml中的方法
            String mappedStatementMethod = "selectByExampleWithCustom";
            // mappedStatementId为具体指向的方法
            String mappedStatementId = mappedStatementNamespace + "." + mappedStatementMethod;

            // mappedStatementParameter可以为mybatis自动生成的XxxExample实体类，例如UserExample
            Object mappedStatementParameter = subjectExample;
            //数字字典固定的值==Subject_exportExcel
            //String exportDictGroupKey = DataDictUtil.DICT_GROUP_KEY_WITH_CAPACITY_EXPORTEXCEL;
            String exportDictGroupKey = DataDictUtil.DICT_GROUP_KEY_WITH_SUBJECT_EXPORTEXCEL;
            //String exportDictGroupKey = "Subject_exportExcel";
            createFileInfo = excelCustomTemplateService.createXlsxToWebDir(
                    sqlSession
                    , mappedStatementId
                    , mappedStatementParameter
                    , exportDictGroupKey
            );
        } catch (Exception e) {
            LOGGER.error("{}error;", logInfo, e);
            throw e;
        }

        return createFileInfo;
    }
    /**
     * 创建查询Example
     *
     * @param reqEntity reqEntity
     * @param <T>       T
     * @return Example
     */
    private <T extends Subject> SubjectExample createQueryExample(T reqEntity) {
        // mybatis查询方式
        SubjectExample subjectExample = new SubjectExample();
        // 设置排序字段
        // queryExample.setOrderByClause("order_index");
        // 设置查询条件
        // 项目代码
        // 项目名称
        // 勾选项目----多个id
        if (ListUtil.getListSizeWith0(reqEntity.getIds()) > 0) {
            // queryExampleCriteria.andProjectNameLike(SqlUtil.valueJoinLikeBoth(reqEntity.getProjectName()));
            // addCriterion("project_name like", value, "projectName");
            subjectExample.setIds(reqEntity.getIds());
        }
        return subjectExample;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public <T extends Subject> AjaxResult importFromExcel(UserInfo loginUserInfo, Locale reqLocale, MultipartFile reqFile) throws Exception {
        final String logInfo = "[importFromExcel_UUID_" + UUID.randomUUID() + "]";
        AjaxResult ajaxResult = null;
        XSSFWorkbook workbook = null;
        InputStream reqFileInputStream = null;

        try {
            if (reqFile == null) {
                return AjaxResultUtil.createErrorMessage("文件不存在");
            }

            boolean reqFileEmpty = reqFile.isEmpty();
            String reqFileName = reqFile.getName();
            String reqFileOriginalFilename = reqFile.getOriginalFilename();

            LOGGER.info("{}reqFileEmpty:{};reqFileName:{};reqFileOriginalFilename:{};"
                    , logInfo
                    , reqFileEmpty, reqFileName, reqFileOriginalFilename
            );

            if (!reqFileOriginalFilename.toLowerCase().endsWith(".xlsx".toLowerCase(Locale.ENGLISH))) {
                LOGGER.error("{}只支持.xlsx文件。", logInfo);
                return AjaxResultUtil.createErrorMessage("只支持.xlsx文件", null);
            }

            reqFileInputStream = reqFile.getInputStream();
            workbook = new XSSFWorkbook(reqFileInputStream);
            Sheet sheet = workbook.getSheetAt(0);

            Iterator<Row> itRow = sheet.rowIterator();
            int rowIndex = 0;
            Row row = null;
            Cell cell000 = null;
            Cell cell001 = null;
            Cell cell002 = null;
            Cell cell003 = null;
            Cell cell004 = null;
            Cell cell005 = null;
            Cell cell006 = null;
            Cell cell007 = null;
            Cell cell008 = null;
            Cell cell009 = null;
            String cellValue000 = null;
            String cellValue001 = null;
            String cellValue002 = null;
            String cellValue003 = null;
            String cellValue004 = null;
            String cellValue005 = null;
            //String cellValue006 = null;
            //String cellValue007 = null;
            String cellValue008 = null;
            String cellValue009 = null;

            final List<T> reqEntityList = new ArrayList<>();
            T reqEntity = null;
            SimpleDateFormat sdfYyyymmddHhmmss = new SimpleDateFormat(DateUtil.KEY_YYYYMMDD_HHMMSS_INCLUDE_SPLIT_CHAR);
            SimpleDateFormat sdfYyyymmdd = new SimpleDateFormat(DateUtil.KEY_YYYYMMDD_INCLUDE_SPLIT_CHAR);

            while (itRow.hasNext()) {
                row = itRow.next();

                // 第1行为标题
                if (rowIndex++ <= 0) {
                    continue;
                }

                // 课题编码
                // 课题名称
                // 课题级别
                // 立项年份
                // 专业分部
                // 课题负责人
                // 开始日期
                // 结束日期
                // 获奖情况
                // 资产名称
                cell000 = row.getCell(0);
                cell001 = row.getCell(1);
                cell002 = row.getCell(2);
                cell003 = row.getCell(3);
                cell004 = row.getCell(4);
                cell005 = row.getCell(5);
                cell006 = row.getCell(6);
                cell007 = row.getCell(7);
                cell008 = row.getCell(8);
                cell009 = row.getCell(9);

                cellValue000 = ExcelUtil.getCellValue(cell000);
                cellValue001 = ExcelUtil.getCellValue(cell001);
                cellValue002 = ExcelUtil.getCellValue(cell002);
                cellValue003 = ExcelUtil.getCellValue(cell003);
                cellValue004 = ExcelUtil.getCellValue(cell004);
                cellValue005 = ExcelUtil.getCellValue(cell005);
                //cellValue006 = ExcelUtil.getCellValue(cell006);
                Date dateCellValue6 = cell006.getDateCellValue();
                Date dateCellValue7 = cell007.getDateCellValue();
                cellValue008 = ExcelUtil.getCellValue(cell008);
                cellValue009 = ExcelUtil.getCellValue(cell009);

                reqEntity = (T) new Subject();
                reqEntityList.add(reqEntity);

                // project_code 课题编码
                reqEntity.setSubjectCode(cellValue000);
                // project_name 课题名称
                reqEntity.setSubjectName(cellValue001);
                // level 课题级别
                //reqEntity.setLevel(cellValue002);
                List<DataDict> dataDictslevel = dataDictService.queryByDictGroupKey("sci_topic_level");
                for (DataDict dataDict : dataDictslevel) {
                    if (dataDict.getDictKey().equals(cellValue002)){
                        reqEntity.setLevel(dataDict.getDictText());
                    }
                }
                if (reqEntity.getLevel() == null){
                    throw new RuntimeException("sci_topic_level,Value for condition cannot be null");
                }
                // description 立项年份
                //NumberUtil.parseStringToIntegerWithNull(cellValue007)
                //DateUtil.parseToDate(sdfYyyymmddHhmmss, sdfYyyymmdd, cellValue009)
                reqEntity.setYear(NumberUtil.parseStringToIntegerWithNull(cellValue003));
                // dept_name 专业分部名称
                reqEntity.setDeptName(cellValue004);
                // dept_id 数字字典===专业分部
                List<DataDict> dataDicts = dataDictService.queryByDictGroupKey("achievement_profession_dept");
                for (DataDict dataDict : dataDicts) {
                    if (dataDict.getDictText().equals(cellValue004)){
                        reqEntity.setDeptId(dataDict.getId());
                    }
                }
                //专业分部
                if(reqEntity.getDeptId() == "" ){
                    throw new RuntimeException("Value for condition cannot be null");
                }

                // subject_user 课题负责人
                reqEntity.setSubjectUser(cellValue005);
                // StartTime 开始日期 HSSFDateUtil
                reqEntity.setStartTime(dateCellValue6);
                // EndTime 结束日期
                reqEntity.setEndTime(dateCellValue7);
                // FileName 获奖证书名称
                reqEntity.setFileName(cellValue008);
                // assets_name 资产名称
                reqEntity.setAssetsName(cellValue009);
                if (ListUtil.getListSizeWith0(reqEntityList) >= SqlUtil.INSERT_BATCH_MAX_ROWS) {
                    saveOrUpdate(loginUserInfo, reqLocale, reqEntityList);
                    reqEntityList.clear();
                }
            }

            if (ListUtil.getListSizeWith0(reqEntityList) > 0) {
                saveOrUpdate(loginUserInfo, reqLocale, reqEntityList);
                reqEntityList.clear();
            }
            ajaxResult = AjaxResultUtil.createSuccessMessage("保存成功");
        } catch (Exception e) {
            LOGGER.error("{}error;", logInfo, e);
            throw e;
        } finally {
            IOUtil.closeStream(reqFileInputStream);
            ExcelUtil.closeWorkbook(workbook);
        }

        return ajaxResult;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public <T extends Subject> AjaxResult saveOrUpdate(UserInfo loginUserInfo, Locale reqLocale, List<T> reqEntityList) {
        final String logInfo = "[PurchaseProject_saveOrUpdate_UUID_" + UUID.randomUUID() + "]";
        AjaxResult ajaxResult = null;

        try {
            Date nowDate = new Date();

            // begin 获取登录用户的信息
            String loginUserInfoUserId = loginUserInfo.getUserId();
            String loginUserInfoUserName = loginUserInfo.getUserName();
            // end 获取登录用户的信息

            //PurchaseProject dbPurchaseProject = null;
            Subject usedEntity = null;
            int saveRows = 0;
            int updateRows = 0;

            String reqId = null;
            String reqProjectCode = null;

            for (Subject reqEntity : reqEntityList) {
                reqId = reqEntity.getId();
                reqProjectCode = reqEntity.getSubjectCode();

                // dbPurchaseProject = purchaseProjectMapper.selectByPrimaryKey(reqId);

                if (StringUtil.isNullOrSpace(reqId)) {
                    // 保存
                    usedEntity = new Subject();
                    usedEntity.setId(IDHelper.uuidNew());
                    usedEntity.setSubjectCode(reqProjectCode);
                    usedEntity.setSubjectName(reqEntity.getSubjectName());
                    usedEntity.setLevel(reqEntity.getLevel());
                    usedEntity.setYear(reqEntity.getYear());
                    usedEntity.setDeptId(reqEntity.getDeptId());
                    usedEntity.setDeptName(reqEntity.getDeptName());
                    usedEntity.setSubjectUser(reqEntity.getSubjectUser());
                    usedEntity.setFileName(reqEntity.getFileName());
                    usedEntity.setAssetsName(reqEntity.getAssetsName());
                    usedEntity.setStartTime(reqEntity.getStartTime());
                    usedEntity.setEndTime(reqEntity.getEndTime());
                    usedEntity.setCreateTime(nowDate);
                    usedEntity.setCreateUser(loginUserInfoUserId);
                    usedEntity.setCreateUserName(loginUserInfoUserName);
                    usedEntity.setLastUpdateTime(nowDate);
                    usedEntity.setLastUpdateUser(loginUserInfoUserId);
                    usedEntity.setLastUpdateUserName(loginUserInfoUserName);
                    saveRows += subjectMapper.insert(usedEntity);
                } else {
                    // 修改
                    usedEntity = new Subject();
                    usedEntity.setId(reqId);
                    usedEntity.setSubjectCode(reqProjectCode);
                    usedEntity.setSubjectName(reqEntity.getSubjectName());
                    usedEntity.setLevel(reqEntity.getLevel());
                    usedEntity.setYear(reqEntity.getYear());
                    // usedEntity.setCreateTime(nowDate);
                    // usedEntity.setCreateUser(loginUserInfoUserId);
                    // usedEntity.setCreateUserName(loginUserInfoUserName);
                    usedEntity.setLastUpdateTime(nowDate);
                    usedEntity.setLastUpdateUser(loginUserInfoUserId);
                    usedEntity.setLastUpdateUserName(loginUserInfoUserName);

                    updateRows += subjectMapper.updateById(usedEntity);
                }
            }

            String strResultInfo = "保存" + saveRows + "行；修改" + updateRows + "行；";

            ajaxResult = AjaxResultUtil.createSuccessMessage(strResultInfo);
        } catch (Exception e) {
            LOGGER.error("{}saveOrUpdate.error;", logInfo, e);
            throw e;
        }

        return ajaxResult;
    }



}
